/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package gla.centralhotel.uiweb.secure;

import gla.centralhotel.uiweb.SessionBean;
import java.io.*;
import javax.servlet.http.*;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 *
 * @author Rivo
 */
public class SecureAccessPages extends HttpServlet implements Filter {

   @Override
    public void init(FilterConfig arg0) throws ServletException {
        
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        
        //System.out.println("--FILTER IN --------------" + ((HttpServletRequest) req).getRequestURI()+"   "+((HttpServletRequest) req).getContextPath());
        
        if (((HttpServletRequest) req).getRequestURI().startsWith( ((HttpServletRequest) req).getContextPath() + "/uiweb/screens/")) {
            
            SessionBean s = (SessionBean) ((HttpServletRequest) req).getSession().getAttribute("SessionBean");

            if (s == null || s.getUserLogged() == null) {
                req.getRequestDispatcher("/uiweb/Login.jsp").forward(req, resp);
                return;
            }
        }
        
        chain.doFilter(req, resp);
        
    }

    @Override
    public void destroy() {
    }
}